// pages/userInfoCenter/index.js
const App = getApp();
const { Dialog, Toast } = require("../../../../../config/modal")
const utils = require("../../../../../utils/util")
Page({
    /**
     * 页面的初始数据
     */
    data: {
        offsetTop: App.globalData.window.navHeight,
        theamTextColor: App.globalData.theamTextColor,
        capsuleBarHeight: App.$getMenuButtonHeight(),
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        product: {
            list: [],
            activeKey: 0,
            goodsType: [],
        },
        page: {
            pageNum: 1,
            pageSize: 15,
            pages: 0,
        },
        searchParams: {
            typeNos: '',
        },
        startDongTai: App.$getActionFlag(),
    },
    URL: {
        getList: "/product/getMyProduct",
        productType: "/product/productTypes",
        productImages: "/product/getProductImg/?",
        updateStatus: "/product/updateStatus",
        polish: "/product/polish",
        delete: "/product/delete/?",
    },
    page: {
        progress: "/pages/tabbar/user/integral/progress/index",
        GoodsDetail: "/pages/tabbar/user/integral/exchange/goods-detail/index",
        records: "/pages/tabbar/user/integral/exchange/records/index",
        target: "/pages/tabbar/user/integral/exchange/index",
        home: "/pages/tabbar/index/index",
        add: "/pages/tabbar/user/userInfoCenter/myProduct/add/index",
        userInfoCenter: "/pages/tabbar/user/userInfoCenter/index"
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.hideShareMenu();
        this.initProductType();
        this.setData({
            startDongTai: App.$getActionFlag()
        })
    },



    onShow() {
        this.setData({
            'product.activeKey': 0
        })
    },

    initProductType() {
        App.$get(true, this.URL.productType, {}, res => {
            if (res.R) {
                let data = [{ value: '', name: '全部' }]
                res.data.forEach(i => {
                    data.push(i);
                })
                this.setData({
                    'product.goodsType': data,
                })
                this.initProduct(this.data.page.pageNum, false);
            }
        })
    },
    initProduct(pageNum, loadMore) {
        let params = {
            pageNum: pageNum,
            pageSize: this.data.page.pageSize,
            typeNos: this.data.searchParams.typeNos
        }
        this.setLoadingMore(loadMore, "loading", "加载中...")
        App.$get(!loadMore ? true : loadMore, this.URL.getList, params, res => {
            this.setData({
                "loadMore.show": false,
            })
            if (res.R) {
                this.setData({
                    "page.pageNum": res.data.pageNum,
                    "page.pageSize": res.data.pageSize,
                    "page.pages": res.data.pages
                })
                if (!loadMore) {
                    this.setData({
                        "product.list": res.data.rows,
                    })
                    return;
                }
                let sourceData = this.data.integral.list;
                let newData = res.data.rows;
                newData.forEach(data => {
                    sourceData.push(data);
                })
                this.setData({
                    "product.list": sourceData,
                })
            }
        })
    },

    onGoodTypeChange(e) {
        let value = (e.detail.name == 0 || e.detail.name == '') ? '' : e.detail.name;
        this.setData({
            'searchParams.typeNos': value
        })
        this.initProduct(1, false);
    },

    // 点击纸飞机进入兑换中心
    btnAddClick() {
        App.$navigateTo(this.page.add, true);
    },
    saleProduct(e) {
        let that = this;
        let options = {
            msg: "该闲置物品是否已售出?",
            sureMsg: "是的",
            success: function () {
                let item = utils.getEvent(e).item;
                let index = utils.getEvent(e).index;
                App.$post(true, that.URL.updateStatus, { productNo: item.productNo }, res => {
                    if (res.R) {
                        let key = 'product.list[' + index + "].status"
                        that.setData({
                            [key]: "SOLD"
                        })
                    }
                })
            }
        };
        App.$alertConfirm(options)
    },
    polishProduct(e) {
        let item = utils.getEvent(e).item;
        App.$post(true, this.URL.polish, { productNo: item.productNo }, res => {
            if (res.R) {
                Toast("商品已擦亮")
            }
        })
    },
    updateProduct(e) {
        App.$navigateTo(this.page.add + "?productNo=" + utils.getEvent(e).item.productNo, true)
    },
    deleteProduct(e) {
        if (e.detail === "right") {
            let that = this;
            let options = {
                msg: "是否确认删除?",
                success: function () {
                    let item = utils.getEvent(e).item;
                    let index = utils.getEvent(e).index;
                    App.$delete(true, that.URL.delete.replace("?", item.productNo), {}, res => {
                        if (res.R) {
                            let list = that.data.product.list;
                            list.splice(index, 1);
                            that.setData({
                                "product.list": list
                            })
                        }
                    })
                }
            };
            App.$alertConfirm(options)
        }
    },

    showLagerImg(e) {
        let imgUrl = utils.getEvent(e).img;
        let item = utils.getEvent(e).item;
        App.$get(false, this.URL.productImages.replace("?", item.productNo), {}, res => {
            let imgs = [imgUrl];
            if (res.R) {
                imgs = [];
                for (let i = 0; i < res.data.length; i++) {
                    imgs.push(res.data[i].imgUrl)
                }
            }
            wx.previewImage({
                current: imgUrl,
                urls: imgs
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    // 返回上一级
    goBack() {
        App.$switchTab(this.page.userInfoCenter, false);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.page.pages === 0 || this.data.page.pageNum + 1 > this.data.page.pages) {
            this.setLoadingMore(true, "end", "已加载全部")
            return;
        }
        this.initIntegralGoods(this.data.page.pageNum + 1, true);
    },
    setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (e) {
        let item = e.target.dataset.item;
        return App.$onShare('/pages/tabbar/index/product/detail/index', "&productNo=" + item.productNo, item.productName)
    },

})
